#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "data_type.h"
#include "alloc.h"
#include "list.h"
#include "attrlist.h"
#include "memfunc.h"
#include "basefunc.h"
#include "struct_deal.h"
#include "crypto_func.h"
#include "basefunc.h"
#include "memdb.h"
#include "message.h"
#include "dispatch.h"
#include "ex_module.h"

#include "sys_func.h"
#include "message_box.h"
#include "route_tree.h"

static ROUTINE * dispatch_proc;

struct router_init_para
{ 
	char * router_file;
	char * aspect_file;
};

int read_dispatch_file(char * file_name,int is_aspect)
{
	int ret;

	int fd;
	int readlen;
	int json_offset;

	int count=0;
	void * root_node;
	void * findlist;
	void * memdb_template ;
	BYTE uuid[DIGEST_SIZE];
	char json_buffer[4097];
	void * policy;
	int finishread=0;
	int leftlen=0;

	fd=open(file_name,O_RDONLY);
	if(fd<0)
		return fd;

	readlen=read(fd,json_buffer,4096);
	if(readlen<0)
		return -EIO;
	json_buffer[readlen]=0;
	if(readlen<4096)
		finishread=1;

	leftlen=readlen;

	json_offset=0;

	while(leftlen>DIGEST_SIZE)
	{
		readlen=json_solve_str(&root_node,json_buffer);
		if(readlen<0)
		{
			print_cubeerr("solve policy %s error!\n",file_name);
			break;
		}
		json_offset+=readlen;
		Memcpy(json_buffer,json_buffer+readlen,leftlen-readlen);
		leftlen-=readlen;
		json_buffer[leftlen]=0;

		if(finishread==0)
		{
			ret=read(fd,json_buffer+leftlen,4096-leftlen);
			if(ret<4096-leftlen)
				finishread=1;
			leftlen+=ret;	
		}	
	
		if(readlen<32)
			continue;

		policy=route_read_policy(root_node);
		if(policy==NULL)
		{
			print_cubeerr("read %d file error!",count);
			break;
		}
		if((route_path_getstate(policy)==POLICY_IGNORE)
			||(route_path_getstate(policy)==POLICY_CLOSE))
		{
			char * policyname =route_path_getname(policy);
			print_cubeaudit("policy %s is ignored!",policyname);
		}
		else
		{
			route_add_policy(policy);
			count++;
		}
	}
	
	//print_cubeaudit("read %d policy succeed!",count);
	close(fd);
	return count;
}



int _routine_dispatch_init()
{
	int ret;
	
	return 0;
}

int _routine_dispatch_recv_start()
{
	int ret;
	int count=0;
	void * policy;
	void * match_rule;
	void * route_rule;
	void * msg;
	void * message_list=_get_recv_message_list(NULL);

	do
	{
		ret=list_queue_get(message_list,&msg);
		if(ret<0)
			return ret;
		if(msg==NULL)
			break;
		ret=dispatch_policy_getfirst(&policy);
		if(ret<0)
			return -EINVAL;
		while(policy!=NULL)
		{
			ret=dispatch_match_message(policy,msg);
			if(ret>0)
			{
				ret=dispatch_policy_getfirstrouterule(policy,&route_rule);
//				if(route_rule!=NULL)
//				{
//					
//				}
			}
			ret=dispatch_policy_getnext(&policy);
		}
		
	}while(1);

	return count;
}

int _routine_dispatch_send_start()
{
	int ret;
	int count=0;
	void * policy;
	void * match_rule;
	void * route_rule;
	void * msg;
	void * message_list=_get_send_message_list(NULL);
	do
	{
		ret=list_queue_get(message_list,&msg);
		if(ret<0)
			return ret;
		if(msg==NULL)
			break;
		ret=dispatch_policy_getfirst(&policy);
		if(ret<0)
			return -EINVAL;
		while(policy!=NULL)
		{
			ret=dispatch_match_message(policy,msg);
			if(ret>0)
			{
				ret=dispatch_policy_getfirstrouterule(policy,&route_rule);
//				if(route_rule!=NULL)
//				{
//					
//				}
			}
			ret=dispatch_policy_getnext(&policy);
		}
		
	}while(1);

	return count;
}
